package com.jeeplus.test.jlnu.teachingArchive.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.test.jlnu.graduationInfo.domain.vo.NewGraduationInfoReqVo;
import com.jeeplus.test.jlnu.teachingArchive.domain.entity.AcademicInfo;
import com.jeeplus.test.jlnu.teachingArchive.domain.vo.AcademicInfoVo;
import com.jeeplus.test.jlnu.teachingArchive.service.AcademicInfoService;
import com.jeeplus.test.tools.resp.R;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("composing")
public class AcademicInfoController {

    @Autowired
    private AcademicInfoService academicInfoService;

    @ApiOperation("基本信息分页查询")
    @GetMapping("/page")
    public Page<AcademicInfo> getAcademicInfoPage(AcademicInfoVo academicInfoVo, @RequestParam(defaultValue = "1") int pageNum,
                                                    @RequestParam(defaultValue = "10") int pageSize) {
        Page<AcademicInfo> page = new Page<>(pageNum, pageSize);
        Page<AcademicInfo> pageResult = academicInfoService.getAcademicInfoPage(academicInfoVo, page);
        return pageResult;
    }

    //批量修改信息
    @ApiOperation("批量修改")
    @PostMapping("/batchUpdateById")
    public R batchUpdateById(@RequestBody AcademicInfoVo academicInfoVo) {
        if (academicInfoVo.getIds().isEmpty()) {
            return R.error("9999", "参数为空");
        }
        academicInfoService.batchUpdateById(academicInfoVo, academicInfoVo.getIds());
        return R.suc();
    }

    @ApiOperation("打印模板")
    @GetMapping("/print")
    public void print(@RequestParam @NotBlank(message = "type不能为空") String type,
                      @RequestParam @NotEmpty(message = "ids不能为空") String id,
                      HttpServletResponse response){
        try {
            //返回pdf文件 设置setContentType
            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition", "inline; filename=\"archive.pdf\"");

//            response.setHeader("Content-Disposition", "attachment; filename=\"archive.zip\"");

            academicInfoService.print(type,id,response);
            // 打印业务逻辑实现
        } catch (Exception e) {
            throw new RuntimeException("打印失败", e);
        }
    }
}
